vows   = require 'vows'
assert = require 'assert' 
# _      = require './lib/underscore.js'
suite  = vows.describe('Employee')

class Employee
  constructor: (@name) -> #
  isEmployee: -> true
  isVeteran: -> true
  createProject: (title) -> new Project(title)
  matchName: (namePattern) ->
    names = namePattern.split(" ")
    #   if namePattern instanceof String
    #     namePattern.split(" ") # if itsa string, split to array
    #   else
    #     namePattern # assume array
    # # This will return an array for every match
    for name in names
      if @name.split(" ").any((x) -> x==name)
        return true
    false
class Project
  constructor: (@title) -> #
  rename: (newTitle) -> @title = newTitle
pms = 
  employees: []
  projects: []
  findEmployee: (namePattern) ->
    # emp for emp in employees when emp.matchName(namePattern)
    _.find employees, (emp) -> emp.matchName(namePattern)
  createEmployee: (name) ->
    emp = new Employee(name)
    employees.append emp
    return emp
  # findEmployees: (criterion) ->
  #   underscore.filter employees, (emp) ->
  #     for cr of criterion
  #       switch cr
  #         when "name" then
  #           emp.matchName(criterion.name)

suite.addBatch
  'Add employee':
    topic: ->
      pms.createEmployee("Sobah Petersen")
    
    'has the same name as given': (employee) ->
      assert.equal employee.name, "Sobah Petersen"
    
    "When searching for that person":
      topic: (employee) ->  
        pms.findEmployees("Sobah") # returns an array
      
      'that employee list shant be empty': (employeeList) ->
        assert.isNotEmpty employeeList
  # 'A new Employee':
  #   topic: -> new Employee("Sobah")

  #   'should be an employee': (employee) ->
  #     assert.isTrue employee.isEmployee()

  #   'can create projects':
  #     topic: (employee) -> employee.createProject("sobahprosjektet")
      
  #     'project should have a title': (project) ->
  #       assert.isDefined project.title
      
  #     'that is a string': (project) ->
  #       assert.isString project.title
      
  #     'can be renamed': (project) ->
  #       somename = "somename"
  #       project.rename(somename)
  #       assert.equal(project.title, somename)
  # 'An old employee':
  #   topic: -> Employee.get("Sobah")

  #   'is a veteran': (employee) ->
  #     assert.isTrue employee.isVeteran()
suite.export(module)